﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Kugar.Core.Web;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Kugar.MVC.MobileUI.WeUI.Controllers
{
    public abstract class MobileUIBaseController : Controller
    {
        private RequestLocal<List<VM_MVCMsgBoxItem>> _msgList = null;

        protected MobileUIBaseController()
        {
            _msgList = new RequestLocal<List<VM_MVCMsgBoxItem>>(getMsgList);
        }

        protected void Title(string title)
        {
            ViewBag.Title = title;
        }

        protected void MsgBox(string message)
        {
            MsgBoxAndScript(msg:message);
        }

        protected void MsgBoxAndRefresh(string msg)
        {
            MsgBoxAndGoto(msg,Request.GetDisplayUrl());
        }

        protected void MsgBoxAndGoto(string msg, string url)
        {
            MsgBoxAndScript(msg, $"WebUIJS.GoTo('{url}',this);");
        }

        protected void MsgBoxAndScript(string msg, string script="",string beforceAlertScript="")
        {
            if (string.IsNullOrWhiteSpace(msg) && string.IsNullOrWhiteSpace(script))
            {
                return;
            }

            List<VM_MVCMsgBoxItem> msgList = null;

            if (!_msgList.IsValueCreated)
            {
                TempData["MsgData_Temp"] =_msgList.Value;
            }

            msgList = _msgList.Value;

            msgList.Add(new VM_MVCMsgBoxItemEx(msg, script){BeforceAlertScript = beforceAlertScript });
        }

        protected void MsgBoxAndCloseModal(string msg, bool isRefreshPage = false)
        {
            var modalindex = Request.GetString("modalindex");

            string script;

            if (string.IsNullOrWhiteSpace(modalindex))
            {
                script = "WebUIJS.CloseModalWindow();";
            }
            else
            {
                script = $"WebUIJS.CloseModalWindow('{modalindex}');";
            }

            if (isRefreshPage)
            {
                script += "WebUIJS.Loading(); WebUIJS.Refresh()";
            }

            this.MsgBoxAndScript(msg, script, "WebUIJS.CloseModalWindow();");
        }

        protected void MsgBoxAndCallbackData(string msg, JObject callbackData, bool isCloseModal = false)
        {
            var callbackID = Request.GetString("CallbackID");

            var modalID = Request.GetString("modalid");

            string script;

            if (string.IsNullOrWhiteSpace(modalID))
            {
                script = "WebUIJS.CloseModalWindow();";
            }
            else
            {
                script = $"WebUIJS.CloseModalWindow('{modalID}');";
            }

            if (!string.IsNullOrWhiteSpace(callbackID))
            {
                MsgBoxAndScript(msg, $"__callFuncByID('{callbackID}',{callbackData.ToString(Formatting.None)});{(isCloseModal ? script : "")}");
            }
            else
            {
                MsgBoxAndScript(msg, isCloseModal ? script : "");
            }

        }

        private List<VM_MVCMsgBoxItem> getMsgList()
        {

            return new List<VM_MVCMsgBoxItem>();
        }

        public override ViewResult View(string viewName, object model)
        {
            return base.View(viewName, model);
        }
    }
}